# -*- coding:utf-8 -*-

import threading
import time

"""
多线程
多线程：IO密集型任务
多进程：CPU密集型任务

"""


class myThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
        print("开始线程：" + self.name)
        print_time(self.name, self.counter, 5)
        print("退出线程：" + self.name)


def print_time(threadName, delay, counter):
    while counter:
        if EXIT_FLAG:
            threadName.exit()
        time.sleep(delay)
        print("%s: %s" % (threadName, time.ctime(time.time())))
        counter -= 1


if __name__ == "__main__":
    EXIT_FLAG = 0

    threadID = 1
    threads = []
    threadList = ["Thread-1", "Thread-2"]
    # 创建新线程
    for tName in threadList:
        thread = myThread(threadID, tName, threadID)
        thread.start()
        threads.append(thread)
        threadID += 1
    # 等待所有线程完成
    for t in threads:
        t.join()
    print("退出主线程")

